﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class LocationDA : ILocationDA
    {
        private string ABC = "@ABC";
        private string CHECK_DIG = "@CHECK_DIG";
        private string CREATED_BY = "@CREATED_BY";
        private string CUBE = "@CUBE";
        private string CUBIC_CAPACITY = "@CUBIC_CAPACITY";
        private string FOOT_PRINT = "@FOOT_PRINT";
        private string HEIGHT = "@HEIGHT";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_LOSE_ID = "@IS_LOSE_ID";
        private string IS_MULTI_LOT = "@IS_MULTI_LOT";
        private string IS_MULTI_SKU = "@IS_MULTI_SKU";
        private string LENGTH = "@LENGTH";
        private string LOC = "@LOC";
        private string LOC_CATEGORY = "@LOC_CATEGORY";
        private string LOC_FLAG = "@LOC_FLAG";
        private string LOC_HANDLING = "@LOC_HANDLING";
        private string LOC_LEVEL = "@LOC_LEVEL";
        private string LOC_TEMPLATE_ID = "@LOC_TEMPLATE_ID";
        private string LOC_USAGE = "@LOC_USAGE";
        private string LOGICAL_LOC = "@LOGICAL_LOC";
        private string SQL_CHECK_LOCATION_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LOCATION WHERE LOC = @LOC ";
        private string SQL_DELETE_LOCATION = " DELETE FROM @_@LOCATION WHERE LOC = @LOC  AND SYSTEM_CREATED = 'N' ";
        private string SQL_INSERT_LOCATION = " INSERT INTO @_@LOCATION ( WH_ID, LOC, LOC_TEMPLATE_ID, LOC_USAGE, LOC_CATEGORY, LOC_FLAG, LOC_HANDLING, CHECK_DIG, ABC, STATUS, ZONE_ID, LOGICAL_LOC, IS_MULTI_SKU, IS_MULTI_LOT, IS_LOSE_ID, X_COORD, Y_COORD, Z_COORD, LOC_LEVEL, LENGTH, WIDTH, HEIGHT, CUBE, CUBIC_CAPACITY, WEIGHT_CAPACITY, STACK_LIMIT, FOOT_PRINT, TEMPLATE_FIELD1, TEMPLATE_FIELD2, TEMPLATE_FIELD3, TEMPLATE_FIELD4, TEMPLATE_FIELD5, IS_ACTIVE, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @LOC, @LOC_TEMPLATE_ID, @LOC_USAGE, @LOC_CATEGORY, @LOC_FLAG, @LOC_HANDLING, @CHECK_DIG, @ABC, @STATUS, @ZONE_ID, @LOGICAL_LOC, @IS_MULTI_SKU, @IS_MULTI_LOT, @IS_LOSE_ID, @X_COORD, @Y_COORD, @Z_COORD, @LOC_LEVEL, @LENGTH, @WIDTH, @HEIGHT, @CUBE, @CUBIC_CAPACITY, @WEIGHT_CAPACITY, @STACK_LIMIT, @FOOT_PRINT, @TEMPLATE_FIELD1, @TEMPLATE_FIELD2, @TEMPLATE_FIELD3, @TEMPLATE_FIELD4, @TEMPLATE_FIELD5, @IS_ACTIVE, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LOCATION = " SELECT  loc.ROWID, loc.WH_ID, loc.LOC, loc.LOC_TEMPLATE_ID,  loc.LOC_USAGE,  T.[TEXT] AS T_LOC_USAGE,  loc.LOC_CATEGORY,  T1.[TEXT] AS T_LOC_CATEGORY,  loc.LOC_FLAG,  T2.[TEXT] AS T_LOC_FLAG,  loc.LOC_HANDLING,   T3.[TEXT] AS T_LOC_HANDLING,  loc.CHECK_DIG, loc.ABC, loc.STATUS, loc.ZONE_ID, loc.LOGICAL_LOC, loc.IS_MULTI_SKU, loc.IS_MULTI_LOT, loc.IS_LOSE_ID, loc.X_COORD, loc.Y_COORD, loc.Z_COORD, loc.LOC_LEVEL, loc.LENGTH, loc.WIDTH, loc.HEIGHT, loc.CUBE, loc.CUBIC_CAPACITY, loc.WEIGHT_CAPACITY, loc.STACK_LIMIT, loc.FOOT_PRINT, loc.TEMPLATE_FIELD1, loc.TEMPLATE_FIELD2, loc.TEMPLATE_FIELD3, loc.TEMPLATE_FIELD4, loc.TEMPLATE_FIELD5, loc.IS_ACTIVE, loc.SYSTEM_CREATED, loc.TS, loc.CREATED_BY, loc.CREATED_DATE, loc.UPDATED_BY, loc.UPDATED_DATE    FROM  @_@LOCATION loc  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = loc.LOC_USAGE AND SCD.SYS_CODE_TYPE = 'LOCUSAGE'    LEFT JOIN @_@SYS_CODE_DETAIL SCD1 ON SCD1.SYS_CODE = loc.LOC_CATEGORY AND SCD1.SYS_CODE_TYPE = 'LOCCATEGORY'    LEFT JOIN @_@SYS_CODE_DETAIL SCD2 ON SCD2.SYS_CODE = loc.LOC_FLAG AND SCD2.SYS_CODE_TYPE = 'LOCFLAG'    LEFT JOIN @_@SYS_CODE_DETAIL SCD3 ON SCD3.SYS_CODE = loc.LOC_HANDLING AND SCD3.SYS_CODE_TYPE = 'LOCHANDLING'    LEFT JOIN sysadm.TRANSLATION T ON 'LOCUSAGE.'+ loc.LOC_USAGE = T.ITEM_ID          AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE  LEFT JOIN sysadm.TRANSLATION T1 ON 'LOCCATEGORY.'+ loc.LOC_CATEGORY = T1.ITEM_ID       AND  T1.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T1.LANG+'-'+T1.COUNTRY = @LANGUAGE   LEFT JOIN sysadm.TRANSLATION T2 ON 'LOCFLAG.'+ loc.LOC_FLAG = T2.ITEM_ID       AND  T2.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T2.LANG+'-'+T2.COUNTRY = @LANGUAGE   LEFT JOIN sysadm.TRANSLATION T3 ON 'LOCHANDLING.'+ loc.LOC_HANDLING = T3.ITEM_ID       AND  T3.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T3.LANG+'-'+T3.COUNTRY = @LANGUAGE ";
        private string SQL_SELECT_ALL_LOCATION_BY_ID = " SELECT  ROWID, WH_ID, LOC, LOC_TEMPLATE_ID, LOC_USAGE, LOC_CATEGORY, LOC_FLAG, LOC_HANDLING, CHECK_DIG, ABC, STATUS, ZONE_ID, LOGICAL_LOC, IS_MULTI_SKU, IS_MULTI_LOT, IS_LOSE_ID, X_COORD, Y_COORD, Z_COORD, LOC_LEVEL, LENGTH, WIDTH, HEIGHT, CUBE, CUBIC_CAPACITY, WEIGHT_CAPACITY, STACK_LIMIT, FOOT_PRINT, TEMPLATE_FIELD1, TEMPLATE_FIELD2, TEMPLATE_FIELD3, TEMPLATE_FIELD4, TEMPLATE_FIELD5, IS_ACTIVE, SYSTEM_CREATED, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@LOCATION ";
        private string SQL_UPDATE_LOCATION = " UPDATE @_@LOCATION SET WH_ID = @WH_ID, LOC_TEMPLATE_ID = @LOC_TEMPLATE_ID, LOC_USAGE = @LOC_USAGE, LOC_CATEGORY = @LOC_CATEGORY, LOC_FLAG = @LOC_FLAG, LOC_HANDLING = @LOC_HANDLING, CHECK_DIG = @CHECK_DIG, ABC = @ABC, STATUS = @STATUS, ZONE_ID = @ZONE_ID, LOGICAL_LOC = @LOGICAL_LOC, IS_MULTI_SKU = @IS_MULTI_SKU, IS_MULTI_LOT = @IS_MULTI_LOT, IS_LOSE_ID = @IS_LOSE_ID, X_COORD = @X_COORD, Y_COORD = @Y_COORD, Z_COORD = @Z_COORD, LOC_LEVEL = @LOC_LEVEL, LENGTH = @LENGTH, WIDTH = @WIDTH, HEIGHT = @HEIGHT, CUBE = @CUBE, CUBIC_CAPACITY = @CUBIC_CAPACITY, WEIGHT_CAPACITY = @WEIGHT_CAPACITY, STACK_LIMIT = @STACK_LIMIT, FOOT_PRINT = @FOOT_PRINT, TEMPLATE_FIELD1 = @TEMPLATE_FIELD1, TEMPLATE_FIELD2 = @TEMPLATE_FIELD2, TEMPLATE_FIELD3 = @TEMPLATE_FIELD3, TEMPLATE_FIELD4 = @TEMPLATE_FIELD4, TEMPLATE_FIELD5 = @TEMPLATE_FIELD5, IS_ACTIVE = @IS_ACTIVE, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LOC = @LOC  ";
        private string SQL_UPDATE_LOCATION_STATUS = " UPDATE @_@LOCATION SET STATUS = @STATUS, UPDATED_BY = @UPDATED_BY ,UPDATED_DATE = GETDATE() WHERE LOC = @LOC  ";
        private string STACK_LIMIT = "@STACK_LIMIT";
        private string STATUS = "@STATUS";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TEMPLATE_FIELD1 = "@TEMPLATE_FIELD1";
        private string TEMPLATE_FIELD2 = "@TEMPLATE_FIELD2";
        private string TEMPLATE_FIELD3 = "@TEMPLATE_FIELD3";
        private string TEMPLATE_FIELD4 = "@TEMPLATE_FIELD4";
        private string TEMPLATE_FIELD5 = "@TEMPLATE_FIELD5";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WEIGHT_CAPACITY = "@WEIGHT_CAPACITY";
        private string WH_ID = "@WH_ID";
        private string WIDTH = "@WIDTH";
        private string X_COORD = "@X_COORD";
        private string Y_COORD = "@Y_COORD";
        private string Z_COORD = "@Z_COORD";
        private string ZONE_ID = "@ZONE_ID";

        public bool CheckLocationIDUnique(string locationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOCATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLocationIDUnique(DataBase dataBase, DbTransaction tran, string locationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LOCATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLocation(List<string> locationID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locationID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOCATION.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteLocation(DataBase dataBase, DbTransaction tran, List<string> locationID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locationID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOCATION.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int ExecuteLocationHold(DataBase dataBase, DbTransaction tran, string status, string updateBy, string locID, string whLoginID)
        {
            int result = 0;
            try
            {
                string sql = string.Empty;
                sql = this.SQL_UPDATE_LOCATION_STATUS.Replace("@_@", whLoginID);
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATUS", status), new SqlParameter("@UPDATED_BY", updateBy), new SqlParameter("@LOC", locID) };
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLocation(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLocation(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public LocationInfo GetLocationByID(string locationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION_BY_ID.Replace("@_@", whLoginID) + " WHERE LOC = @LOC ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID) };
            LocationInfo locationInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    locationInfo = new LocationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC"].ToString(), reader["LOC_TEMPLATE_ID"].ToString(), reader["LOC_USAGE"].ToString(), reader["LOC_CATEGORY"].ToString(), reader["LOC_FLAG"].ToString(), reader["LOC_HANDLING"].ToString(), reader["CHECK_DIG"].ToString(), reader["ABC"].ToString(), reader["STATUS"].ToString(), reader["ZONE_ID"].ToString(), reader["LOGICAL_LOC"].ToString(), reader["IS_MULTI_SKU"].ToString(), reader["IS_MULTI_LOT"].ToString(), reader["IS_LOSE_ID"].ToString(), (reader["X_COORD"].ToString() != "") ? int.Parse(reader["X_COORD"].ToString()) : 0, (reader["Y_COORD"].ToString() != "") ? int.Parse(reader["Y_COORD"].ToString()) : 0, (reader["Z_COORD"].ToString() != "") ? int.Parse(reader["Z_COORD"].ToString()) : 0, (reader["LOC_LEVEL"].ToString() != "") ? int.Parse(reader["LOC_LEVEL"].ToString()) : 0, (reader["LENGTH"].ToString() != "") ? ((double) float.Parse(reader["LENGTH"].ToString())) : ((double) 0f), (reader["WIDTH"].ToString() != "") ? ((double) float.Parse(reader["WIDTH"].ToString())) : ((double) 0f), (reader["HEIGHT"].ToString() != "") ? ((double) float.Parse(reader["HEIGHT"].ToString())) : ((double) 0f), (reader["CUBE"].ToString() != "") ? ((double) float.Parse(reader["CUBE"].ToString())) : ((double) 0f), (reader["CUBIC_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["CUBIC_CAPACITY"].ToString())) : ((double) 0f), (reader["WEIGHT_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["WEIGHT_CAPACITY"].ToString())) : ((double) 0f), (reader["STACK_LIMIT"].ToString() != "") ? int.Parse(reader["STACK_LIMIT"].ToString()) : 0, (reader["FOOT_PRINT"].ToString() != "") ? int.Parse(reader["FOOT_PRINT"].ToString()) : 0, reader["TEMPLATE_FIELD1"].ToString(), reader["TEMPLATE_FIELD2"].ToString(), reader["TEMPLATE_FIELD3"].ToString(), reader["TEMPLATE_FIELD4"].ToString(), reader["TEMPLATE_FIELD5"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), "", "", "", "");
                }
            }
            return locationInfo;
        }

        public LocationInfo GetLocationByID(string locationID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID) + " WHERE LOC = @LOC  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            LocationInfo locationInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    locationInfo = new LocationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC"].ToString(), reader["LOC_TEMPLATE_ID"].ToString(), reader["LOC_USAGE"].ToString(), reader["LOC_CATEGORY"].ToString(), reader["LOC_FLAG"].ToString(), reader["LOC_HANDLING"].ToString(), reader["CHECK_DIG"].ToString(), reader["ABC"].ToString(), reader["STATUS"].ToString(), reader["ZONE_ID"].ToString(), reader["LOGICAL_LOC"].ToString(), reader["IS_MULTI_SKU"].ToString(), reader["IS_MULTI_LOT"].ToString(), reader["IS_LOSE_ID"].ToString(), (reader["X_COORD"].ToString() != "") ? int.Parse(reader["X_COORD"].ToString()) : 0, (reader["Y_COORD"].ToString() != "") ? int.Parse(reader["Y_COORD"].ToString()) : 0, (reader["Z_COORD"].ToString() != "") ? int.Parse(reader["Z_COORD"].ToString()) : 0, (reader["LOC_LEVEL"].ToString() != "") ? int.Parse(reader["LOC_LEVEL"].ToString()) : 0, (reader["LENGTH"].ToString() != "") ? ((double) float.Parse(reader["LENGTH"].ToString())) : ((double) 0f), (reader["WIDTH"].ToString() != "") ? ((double) float.Parse(reader["WIDTH"].ToString())) : ((double) 0f), (reader["HEIGHT"].ToString() != "") ? ((double) float.Parse(reader["HEIGHT"].ToString())) : ((double) 0f), (reader["CUBE"].ToString() != "") ? ((double) float.Parse(reader["CUBE"].ToString())) : ((double) 0f), (reader["CUBIC_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["CUBIC_CAPACITY"].ToString())) : ((double) 0f), (reader["WEIGHT_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["WEIGHT_CAPACITY"].ToString())) : ((double) 0f), (reader["STACK_LIMIT"].ToString() != "") ? int.Parse(reader["STACK_LIMIT"].ToString()) : 0, (reader["FOOT_PRINT"].ToString() != "") ? int.Parse(reader["FOOT_PRINT"].ToString()) : 0, reader["TEMPLATE_FIELD1"].ToString(), reader["TEMPLATE_FIELD2"].ToString(), reader["TEMPLATE_FIELD3"].ToString(), reader["TEMPLATE_FIELD4"].ToString(), reader["TEMPLATE_FIELD5"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["T_LOC_USAGE"].ToString(), reader["T_LOC_CATEGORY"].ToString(), reader["T_LOC_FLAG"].ToString(), reader["T_LOC_HANDLING"].ToString());
                }
            }
            return locationInfo;
        }

        public LocationInfo GetLocationByID(DataBase dataBase, DbTransaction tran, string locationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION_BY_ID.Replace("@_@", whLoginID) + " WHERE LOC = @LOC ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID) };
            LocationInfo locationInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                locationInfo = new LocationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC"].ToString(), reader["LOC_TEMPLATE_ID"].ToString(), reader["LOC_USAGE"].ToString(), reader["LOC_CATEGORY"].ToString(), reader["LOC_FLAG"].ToString(), reader["LOC_HANDLING"].ToString(), reader["CHECK_DIG"].ToString(), reader["ABC"].ToString(), reader["STATUS"].ToString(), reader["ZONE_ID"].ToString(), reader["LOGICAL_LOC"].ToString(), reader["IS_MULTI_SKU"].ToString(), reader["IS_MULTI_LOT"].ToString(), reader["IS_LOSE_ID"].ToString(), (reader["X_COORD"].ToString() != "") ? int.Parse(reader["X_COORD"].ToString()) : 0, (reader["Y_COORD"].ToString() != "") ? int.Parse(reader["Y_COORD"].ToString()) : 0, (reader["Z_COORD"].ToString() != "") ? int.Parse(reader["Z_COORD"].ToString()) : 0, (reader["LOC_LEVEL"].ToString() != "") ? int.Parse(reader["LOC_LEVEL"].ToString()) : 0, (reader["LENGTH"].ToString() != "") ? ((double) float.Parse(reader["LENGTH"].ToString())) : ((double) 0f), (reader["WIDTH"].ToString() != "") ? ((double) float.Parse(reader["WIDTH"].ToString())) : ((double) 0f), (reader["HEIGHT"].ToString() != "") ? ((double) float.Parse(reader["HEIGHT"].ToString())) : ((double) 0f), (reader["CUBE"].ToString() != "") ? ((double) float.Parse(reader["CUBE"].ToString())) : ((double) 0f), (reader["CUBIC_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["CUBIC_CAPACITY"].ToString())) : ((double) 0f), (reader["WEIGHT_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["WEIGHT_CAPACITY"].ToString())) : ((double) 0f), (reader["STACK_LIMIT"].ToString() != "") ? int.Parse(reader["STACK_LIMIT"].ToString()) : 0, (reader["FOOT_PRINT"].ToString() != "") ? int.Parse(reader["FOOT_PRINT"].ToString()) : 0, reader["TEMPLATE_FIELD1"].ToString(), reader["TEMPLATE_FIELD2"].ToString(), reader["TEMPLATE_FIELD3"].ToString(), reader["TEMPLATE_FIELD4"].ToString(), reader["TEMPLATE_FIELD5"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), "", "", "", "");
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return locationInfo;
        }

        public LocationInfo GetLocationByID(DataBase dataBase, DbTransaction tran, string locationID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID) + " WHERE LOC = @LOC ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC", locationID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            LocationInfo locationInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                locationInfo = new LocationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC"].ToString(), reader["LOC_TEMPLATE_ID"].ToString(), reader["LOC_USAGE"].ToString(), reader["LOC_CATEGORY"].ToString(), reader["LOC_FLAG"].ToString(), reader["LOC_HANDLING"].ToString(), reader["CHECK_DIG"].ToString(), reader["ABC"].ToString(), reader["STATUS"].ToString(), reader["ZONE_ID"].ToString(), reader["LOGICAL_LOC"].ToString(), reader["IS_MULTI_SKU"].ToString(), reader["IS_MULTI_LOT"].ToString(), reader["IS_LOSE_ID"].ToString(), (reader["X_COORD"].ToString() != "") ? int.Parse(reader["X_COORD"].ToString()) : 0, (reader["Y_COORD"].ToString() != "") ? int.Parse(reader["Y_COORD"].ToString()) : 0, (reader["Z_COORD"].ToString() != "") ? int.Parse(reader["Z_COORD"].ToString()) : 0, (reader["LOC_LEVEL"].ToString() != "") ? int.Parse(reader["LOC_LEVEL"].ToString()) : 0, (reader["LENGTH"].ToString() != "") ? ((double) float.Parse(reader["LENGTH"].ToString())) : ((double) 0f), (reader["WIDTH"].ToString() != "") ? ((double) float.Parse(reader["WIDTH"].ToString())) : ((double) 0f), (reader["HEIGHT"].ToString() != "") ? ((double) float.Parse(reader["HEIGHT"].ToString())) : ((double) 0f), (reader["CUBE"].ToString() != "") ? ((double) float.Parse(reader["CUBE"].ToString())) : ((double) 0f), (reader["CUBIC_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["CUBIC_CAPACITY"].ToString())) : ((double) 0f), (reader["WEIGHT_CAPACITY"].ToString() != "") ? ((double) float.Parse(reader["WEIGHT_CAPACITY"].ToString())) : ((double) 0f), (reader["STACK_LIMIT"].ToString() != "") ? int.Parse(reader["STACK_LIMIT"].ToString()) : 0, (reader["FOOT_PRINT"].ToString() != "") ? int.Parse(reader["FOOT_PRINT"].ToString()) : 0, reader["TEMPLATE_FIELD1"].ToString(), reader["TEMPLATE_FIELD2"].ToString(), reader["TEMPLATE_FIELD3"].ToString(), reader["TEMPLATE_FIELD4"].ToString(), reader["TEMPLATE_FIELD5"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["T_LOC_USAGE"].ToString(), reader["T_LOC_CATEGORY"].ToString(), reader["T_LOC_FLAG"].ToString(), reader["T_LOC_HANDLING"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return locationInfo;
        }

        public DataSet GetLocationByQueryList(List<string> sqlWhere, LocationQueryEntity locationQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!locationQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, locationQuery.CurrentPage, locationQuery.PageSize, locationQuery.SortField, locationQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetLocationByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, LocationQueryEntity locationQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_LOCATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!locationQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, locationQuery.CurrentPage, locationQuery.PageSize, locationQuery.SortField, locationQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertLocation(List<LocationInfo> locationInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                foreach (LocationInfo info in locationInfo)
                {
                    try
                    {
                        info.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                        SqlParameter[] paras = this.Set_Location_Parameters(info);
                        if (paras != null)
                        {
                            result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOCATION.Replace("@_@", whLoginID), paras);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertLocation(DataBase dataBase, DbTransaction tran, LocationInfo locationInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                locationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Location_Parameters(locationInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOCATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Location_Parameters(LocationInfo locationInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LOC, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_TEMPLATE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_USAGE, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_CATEGORY, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_FLAG, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_HANDLING, SqlDbType.VarChar, 20), new SqlParameter(this.CHECK_DIG, SqlDbType.VarChar, 20), new SqlParameter(this.ABC, SqlDbType.VarChar, 1), new SqlParameter(this.STATUS, SqlDbType.VarChar, 10), new SqlParameter(this.ZONE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOGICAL_LOC, SqlDbType.VarChar, 20), new SqlParameter(this.IS_MULTI_SKU, SqlDbType.VarChar, 1), new SqlParameter(this.IS_MULTI_LOT, SqlDbType.VarChar, 1), new SqlParameter(this.IS_LOSE_ID, SqlDbType.VarChar, 1), new SqlParameter(this.X_COORD, SqlDbType.Int, 4), 
                new SqlParameter(this.Y_COORD, SqlDbType.Int, 4), new SqlParameter(this.Z_COORD, SqlDbType.Int, 4), new SqlParameter(this.LOC_LEVEL, SqlDbType.Int, 4), new SqlParameter(this.LENGTH, SqlDbType.Float, 8), new SqlParameter(this.WIDTH, SqlDbType.Float, 8), new SqlParameter(this.HEIGHT, SqlDbType.Float, 8), new SqlParameter(this.CUBE, SqlDbType.Float, 8), new SqlParameter(this.CUBIC_CAPACITY, SqlDbType.Float, 8), new SqlParameter(this.WEIGHT_CAPACITY, SqlDbType.Float, 8), new SqlParameter(this.STACK_LIMIT, SqlDbType.Int, 4), new SqlParameter(this.FOOT_PRINT, SqlDbType.Int, 4), new SqlParameter(this.TEMPLATE_FIELD1, SqlDbType.VarChar, 20), new SqlParameter(this.TEMPLATE_FIELD2, SqlDbType.VarChar, 20), new SqlParameter(this.TEMPLATE_FIELD3, SqlDbType.VarChar, 20), new SqlParameter(this.TEMPLATE_FIELD4, SqlDbType.VarChar, 20), new SqlParameter(this.TEMPLATE_FIELD5, SqlDbType.VarChar, 20), 
                new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(locationInfo.WhID))
            {
                paramArray[0].Value = locationInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = locationInfo.Loc;
            if (!string.IsNullOrEmpty(locationInfo.LocTemplateID))
            {
                paramArray[2].Value = locationInfo.LocTemplateID;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = locationInfo.LocUsage;
            paramArray[4].Value = locationInfo.LocCategory;
            paramArray[5].Value = locationInfo.LocFlag;
            paramArray[6].Value = locationInfo.LocHandling;
            if (!string.IsNullOrEmpty(locationInfo.CheckDig))
            {
                paramArray[7].Value = locationInfo.CheckDig;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            paramArray[8].Value = locationInfo.Abc;
            paramArray[9].Value = locationInfo.Status;
            paramArray[10].Value = locationInfo.ZoneID;
            paramArray[11].Value = locationInfo.LogicalLoc;
            paramArray[12].Value = locationInfo.IsMultiSku;
            paramArray[13].Value = locationInfo.IsMultiLot;
            paramArray[14].Value = locationInfo.IsLoseID;
            paramArray[15].Value = locationInfo.XCoord;
            paramArray[0x10].Value = locationInfo.YCoord;
            paramArray[0x11].Value = locationInfo.ZCoord;
            paramArray[0x12].Value = locationInfo.LocLevel;
            paramArray[0x13].Value = locationInfo.Length;
            paramArray[20].Value = locationInfo.Width;
            paramArray[0x15].Value = locationInfo.Height;
            paramArray[0x16].Value = locationInfo.Cube;
            paramArray[0x17].Value = locationInfo.CubicCapacity;
            paramArray[0x18].Value = locationInfo.WeightCapacity;
            paramArray[0x19].Value = locationInfo.StackLimit;
            paramArray[0x1a].Value = locationInfo.FootPrint;
            if (!string.IsNullOrEmpty(locationInfo.TemplateField1))
            {
                paramArray[0x1b].Value = locationInfo.TemplateField1;
            }
            else
            {
                paramArray[0x1b].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locationInfo.TemplateField2))
            {
                paramArray[0x1c].Value = locationInfo.TemplateField2;
            }
            else
            {
                paramArray[0x1c].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locationInfo.TemplateField3))
            {
                paramArray[0x1d].Value = locationInfo.TemplateField3;
            }
            else
            {
                paramArray[0x1d].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locationInfo.TemplateField4))
            {
                paramArray[30].Value = locationInfo.TemplateField4;
            }
            else
            {
                paramArray[30].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locationInfo.TemplateField5))
            {
                paramArray[0x1f].Value = locationInfo.TemplateField5;
            }
            else
            {
                paramArray[0x1f].Value = DBNull.Value;
            }
            paramArray[0x20].Value = locationInfo.IsActive;
            paramArray[0x21].Value = locationInfo.SystemCreated;
            paramArray[0x22].Value = locationInfo.CreatedBy;
            paramArray[0x23].Value = locationInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLocation(LocationInfo locationInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locationInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Location_Parameters(locationInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOCATION.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateLocation(DataBase dataBase, DbTransaction tran, LocationInfo locationInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locationInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Location_Parameters(locationInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOCATION.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

